package com.thread;

import java.util.concurrent.CompletableFuture;

/**
 * 描述:
 *
 * @author luzhongfu
 * Created by lucky on 2020-05-09 4:35 PM
 */
public class CompletableFutureTest {

    CompletableFuture<Integer> test01(){
        CompletableFuture<Integer> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
            int k = 0;
            for (; k < 99999; k++) {
            }
            return k;
        });
        return voidCompletableFuture;
    }

    CompletableFuture<String> test02(){
        return CompletableFuture.supplyAsync(()->{
            int a = 1/0;
            return "hahah";
        });
    }

    public static void main(String[] args) throws Exception{
        CompletableFutureTest test = new CompletableFutureTest();
        CompletableFuture<Integer> integerCompletableFuture = test.test01();
        Integer integer = integerCompletableFuture.get();
        System.out.println(integer);

        CompletableFuture<String> stringCompletableFuture = test.test02();
        stringCompletableFuture.whenComplete((result,exception)->{
            if(exception == null){
                System.out.println(result);
            }else{
                System.out.println("异常 "+exception.getMessage());
            }
        });
    }
}
